package com.yogee.topseller.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.activity.my.AddAddressActivity;
import com.yogee.topseller.bean.ContractAddressBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/6/23.
 */

public class ContractAddressManageRvAdapter extends RecyclerView.Adapter {
    private Context context;
    private int TYPE_ITEM = 0, TYPE_BOTTOM = 1;
    private List<ContractAddressBean.AddressListBean> data;
    int unfault = -1;
    int a = -1;//点击自己状态改变
    ClickListener clickListener;
    DeleteListener deleteListener;
    EditListener editListener;

    public void setEditListener(EditListener editListener) {
        this.editListener = editListener;
    }

    public void setDeleteListener(DeleteListener deleteListener) {
        this.deleteListener = deleteListener;
    }

    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public ContractAddressManageRvAdapter(Context context) {
        this.context = context;
        notifyDataSetChanged();
    }

    public void addData(List<ContractAddressBean.AddressListBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == data.size()) {
            return TYPE_BOTTOM;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_cam, parent, false);
            return new ItemHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.bottom_cam, parent, false);
            return new BottomHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ItemHolder) {

            ((ItemHolder) holder).itemCamNameTv.setText(data.get(position).getName());
            ((ItemHolder) holder).itemCamPhoneTv.setText(data.get(position).getPhone());
            ((ItemHolder) holder).itemCamAddressTv.setText(data.get(position).getAddress());

            if ("1".equals(data.get(position).getDefaultAddress())) {
                ((ItemHolder) holder).itemCamChekIv.setImageResource(R.drawable.select1);
            } else {
                ((ItemHolder) holder).itemCamChekIv.setImageResource(R.drawable.unselect1);
            }

            ((ItemHolder) holder).itemCamChekIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickListener.clickDefault(data.get(position).getId(), position);
                }
            });


            ((ItemHolder) holder).itemCamDeleteRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    deleteListener.delete(data.get(position).getId(), position);
                }
            });

            ((ItemHolder) holder).itemCamSettingRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    editListener.edit(data.get(position).getId(), data.get(position),position);
                }
            });

        }
        if (holder instanceof BottomHolder) {
            ((BottomHolder) holder).bottomCamAddTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(new Intent(context, AddAddressActivity.class));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() + 1;
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_cam_name_tv)
        TextView itemCamNameTv;
        @BindView(R.id.item_cam_phone_tv)
        TextView itemCamPhoneTv;
        @BindView(R.id.item_cam_address_tv)
        TextView itemCamAddressTv;
        @BindView(R.id.item_cam_chek_iv)
        ImageView itemCamChekIv;
        @BindView(R.id.item_cam_delete_rl)
        RelativeLayout itemCamDeleteRl;
        @BindView(R.id.item_cam_setting_rl)
        RelativeLayout itemCamSettingRl;
//        @BindView(R.id.item_cam_chek_rl)
//        RelativeLayout chekRl;
        
        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.bottom_cam_add_tv)
        RelativeLayout bottomCamAddTv;

        public BottomHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ClickListener {
        void clickDefault(String id, int position);
    }

    public interface DeleteListener {
        void delete(String id, int position);
    }

    public interface EditListener {
        void edit(String id, ContractAddressBean.AddressListBean addressListBeen,int position);
    }
}
